(function(){
	function popWnd(){}
	popWnd.prototype = {
		constructor : popWnd,
		init : function(wndId,wndTitle,closeCallback){
			var msgBoxDiv = null;
			if(typeof wndId === 'object'){
				msgBoxDiv = wndId;
			}else if(typeof wndId === 'string'){
			    msgBoxDiv = document.getElementById(wndId);
			}
			if(!msgBoxDiv){
				//alert('the windows dont exist.');
				return;
			}
			this.msgBoxCurDiv = msgBoxDiv ;
			this.getBackgroundDiv();
			this.getMsgDiv();
			if(typeof wndTitle === 'string'){
				document.getElementById('wndTitle').innerHTML = wndTitle ;
			}
			if(typeof closeCallback === 'function'){				
				this.closeCallBackFun = closeCallback;
			}
			this.wndParentDiv = msgBoxDiv.parentNode;
			/*this.wndParentDiv.removeChild(msgBoxDiv);
			msgBoxDiv.style.display = 'block';
			this.popWndDiv.appendChild(msgBoxDiv);*/
			this.addEventListener('keyup',this.keyupListener);
			//this.show();
		},
		ready:function(wndId,wndTitle,closeCallback){
			triggerInit = function(){
				if(!window.popWnd){
					window.popWnd = new popWnd();
				}
				window.popWnd.init(wndId,wndTitle,closeCallback);
			};
			DOMContentLoaded = function() {
				if (document.addEventListener ){					
					document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
					triggerInit();
				}else if( document.attachEvent ) {
					// Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
					if ( document.readyState === "complete" ) {
						document.detachEvent( "onreadystatechange", DOMContentLoaded );
						triggerInit();
					}
		        };	
			};
			// Mozilla, Opera and webkit nightlies currently support this event
			if (document.addEventListener ) {
				// Use the handy event callback
				document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
				window.addEventListener( "load", triggerInit, false );
			   // If IE event model is used
			} else if (document.attachEvent ) {
				// ensure firing before onload,
				// maybe late but safe also for iframes
				document.attachEvent( "onreadystatechange", DOMContentLoaded);
				window.attachEvent( "onload", triggerInit);
			}
		},
		getBackgroundDiv : function(){
			var bkDiv = this.backgroundDiv;
			if(bkDiv)
			{
				bkDiv.style.width = document.body.scrollWidth + 'px';
				bkDiv.style.height = document.body.scrollHeight + 'px';
				//bkDiv.style.display = 'block';
				return;
			}
			
			var div = document.createElement('div');
			div.style.position = 'absolute';
			div.style.top = '0px';
			div.style.left = '0px';
			if(document.all)
			{
				div.style.filter = 'Alpha(opacity=20)';
			}else if(navigator.userAgent.indexOf("Chrome") > -1) //�ж��Ƿ�Chrome
			{
				div.style.opacity = 0.2;
			}
			else
			{
				div.style.MozOpacity = 0.2;
			}
			div.style.width = document.body.scrollWidth + 'px';
			div.style.height = document.body.scrollHeight + 'px';
			div.style.backgroundColor = 'black';
			div.style.display = 'none';
			//div.style.display = 'block';
			div.style.zIndex = 100;
			this.backgroundDiv = document.body.appendChild(div);
		},
		getMsgDiv :function(){
			var popWndDiv = this.popWndDiv;
			if(popWndDiv){
				//popWndDiv.style.display = 'block';
				return;
			}
			var div = document.createElement('div');
			div.style.zIndex = 101;
			div.style.position = 'absolute';
			div.style.top = '-10000px';
			div.style.left = '0px';
			div.style.backgroundColor = 'white';
			div.style.border = 'solid 1px blue';
			div.style.padding = '5px';
			var strHTML = '<div style="position:relative; cursor:default; font-size:14px;" onmousedown="popWnd.moveStartEvent(event);">';
			strHTML += '<b id="wndTitle"></b>';
			strHTML += '<a id="wndBtnClose" title="Close" style="font-size:12px; position:absolute; right:5px; top:1px; font-family:Tahoma; text-decoration:none; color:blue;" href="javascript:popWnd.closeWin();">&nbsp;X&nbsp;</a></div>';
			strHTML += '<div style="border-top:solid 1px gray; margin:5px auto;"></div>';
			div.innerHTML = strHTML;
			popWndDiv = document.body.appendChild(div);
			/*if(document.forms.length == 0){
				popWndDiv = document.body.appendChild(div);
			}else{
				popWndDiv = document.forms[0].appendChild(div);
			}*/
			this.popWndDiv = div;
		},
		moveStartEvent: function(evt){
			evt = evt ? evt : (window.event ? window.event : null);
			this.mouseX = evt.clientX;
			this.mouseY = evt.clientY;
			this.wnd_x = parseInt(this.popWndDiv.style.left);
			this.wnd_y = parseInt(this.popWndDiv.style.top);
			this.addEventListener('mousemove',this.moveEvent);
			this.addEventListener('mouseup', this.moveFinishedEvent);
			this.addEventListener('selectstart', this.selectStartEvent);
		},
		addEventListener: function(evt,listener){
			if(document.attachEvent){
				document.attachEvent("on" + evt,listener);
			}else if(document.addEventListener){
				document.addEventListener(evt,listener,false);
			}
		},
		removeEventListener : function(evt,listener){
			if(document.detachEvent){				
				document.detachEvent("on" + evt, listener);
			}else if(document.removeEventListener){
				document.removeEventListener(evt, listener,false);
			}
		},
		keyupListener: function(evt){
			evt = evt ? evt : (window.event ? window.event : null);			
			if(evt.keyCode==27 && document.getElementById('wndBtnClose').style.display != 'none')
			window.popWnd.closeWin();
		},
		moveEvent:function(evt){
			evt = evt ? evt : (window.event ? window.event : null);
			var p = window.popWnd;
			var left = p.wnd_x + (evt.clientX - p.mouseX );
			var top =  p.wnd_y + (evt.clientY -  p.mouseY );
			left = left < 0 ? 0 : left;
			top =  top  < 0 ? 0 : top ;
			left = left + p.popWndDiv.offsetWidth < p.backgroundDiv.offsetWidth ? 
			                left : (p.backgroundDiv.offsetWidth -  p.popWndDiv.offsetWidth);
			top = top + p.popWndDiv.offsetHeight < p.backgroundDiv.offsetHeight ? 
			                 top : (p.backgroundDiv.offsetHeight - p.popWndDiv.offsetHeight);
			p.popWndDiv.style.left = left + 'px';
			p.popWndDiv.style.top  = top  + 'px';
		},
		moveFinishedEvent: function(){
			var p = window.popWnd;
			p.removeEventListener('mousemove',  p.moveEvent);
			p.removeEventListener('mouseup',    p.moveFinishedEvent);
			p.removeEventListener('selectstart',p.selectStartEvent);
		},
		selectStartEvent: function(){
			return false;
		},		
		getPageSize: function() {
			var obj = new Object();
			obj.allWidth = document.body.scrollWidth;
			obj.allHeight = document.body.scrollHeight;
			if (document.all) {	//��IE
				obj.top = document.body.scrollTop;
				obj.left = document.body.scrollLeft;
				if (document.compatMode === 'CSS1Compat') {
					obj.width = document.documentElement.clientWidth;
					obj.height = document.documentElement.clientHeight;
				}
				else {
					obj.width = document.body.clientWidth;
					obj.height = document.body.clientHeight;
				}
			} else {
				if (document.compatMode === 'CSS1Compat') {
					obj.width = document.documentElement.clientWidth;
					obj.height = document.documentElement.clientHeight;
					obj.top = document.documentElement.scrollTop;
					obj.left = document.documentElement.scrollLeft;
				}
				else {
					obj.width = document.body.clientWidth;
					obj.height = document.body.clientHeight;
					obj.top = document.body.scrollTop;
					obj.left = document.body.scrollLeft;
				}
			}
			return obj;
		},
		//�رնԻ���
		closeWin: function(srcElement){
			if(this.closeCallBackFun){
				if(srcElement == document.getElementById('wndBtnClose')){
					this.closeCallBackFun(true);
				}else{
					this.closeCallBackFun();
				}
			}
			var popWndDiv = this.popWndDiv;
			popWndDiv.style.width = 'auto';
			popWndDiv.style.height = 'auto';
			popWndDiv.style.display = 'none';
			this.backgroundDiv.style.display = 'none';
			popWndDiv.removeChild(this.msgBoxCurDiv);
			this.wndParentDiv.appendChild(this.msgBoxCurDiv);
			this.msgBoxCurDiv.style.display = 'none';
			this.moveFinishedEvent();
		},
		show : function(){
			wndDiv = this.popWndDiv;	
			msgBoxDiv = this.msgBoxCurDiv ;
			this.backgroundDiv.style.display = 'block';
			this.wndParentDiv.removeChild(msgBoxDiv);
			msgBoxDiv.style.display = 'block';
			this.popWndDiv.appendChild(msgBoxDiv);
			
			var pageSizeObj = this.getPageSize();
			var top = (pageSizeObj.height - wndDiv.offsetHeight) / 2;
			var left = (pageSizeObj.width - wndDiv.offsetWidth) / 2;
			
			wndDiv.style.top = top < 0 ? pageSizeObj.top : pageSizeObj.top + top + 'px';
			wndDiv.style.left = left < 0 ? pageSizeObj.left : pageSizeObj.left + left + 'px';
			wndDiv.style.width = wndDiv.clientWidth - parseInt(wndDiv.style.paddingLeft) - parseInt(wndDiv.style.paddingRight) + 'px';
			
			var allElement = wndDiv.getElementsByTagName('*');
			for(var i=0; i<allElement.length; i++){
				var element = allElement[i];
				if((element.tagName == 'INPUT' || element.tagName == 'SELECT' || element.tagName == 'TEXTAREA') && !element.disabled)
				{
					element.focus();
					break;
				} 
			}
			wndDiv.style.display = 'block';
		}
	};
	if(typeof window.popWnd ==='undefined'){
		window.popWnd = new popWnd;
	}
})();